package org.mineground.commands.player;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.mineground.Main;
import org.mineground.Utilities;
import org.mineground.player.MinegroundPlayer;

public class OnlineTime implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) throws CommandException {
	Player player = (Player) sender;

	if (args.length == 0) {
	    MinegroundPlayer playerStats = Main.getInstance().getPlayer(player);
	    long onlineTime = (System.currentTimeMillis() - playerStats.getJoinTick()) / 1000L;
	    onlineTime += playerStats.getOnlineSeconds();

	    String time = Utilities.formatTime(onlineTime);
	    player.sendMessage(ChatColor.YELLOW + "* You have been online for " + time + ".");
	    return true;
	}

	Player playerEx = Main.getInstance().getServer().getPlayer(args[0]);

	if (playerEx == null) {
	    player.sendMessage(ChatColor.RED + "* Error: Invalid player.");
	    return true;
	}

	MinegroundPlayer playerStats = Main.getInstance().getPlayer(playerEx);
	long onlineTime = (System.currentTimeMillis() - playerStats.getJoinTick()) / 1000L;
	onlineTime += playerStats.getOnlineSeconds();

	String time = Utilities.formatTime(onlineTime);
	player.sendMessage(ChatColor.YELLOW + "* " + playerEx.getDisplayName() + " has been online for " + time + ".");
	return true;
    }
}